<?php

class RequestController extends Controller {

    public function actionApprove($id) {
        if (Yii::app()->request->isPostRequest) {
            // We only allow deletion via POST request
            
            $model = $this->loadModel($id);
            $model->status = 'approved';
            if (!$model->save()) {
                throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
            }

            // If AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if (!isset($_GET['ajax'])) {
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
            }
        } else {
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }

    public function actionCreate($date="", $time_start="", $time_end="", $participant="", $room_id="") {
        $model = new RapatRequest;
        $ruangRapat = new Ruangan;
        $ruangRapat = $ruangRapat->findAll('type_ruangan_id = 1');

        // uncomment the following code to enable ajax-based validation
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'rapat-reservation-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
        
        $model->date = $date;
        $model->room_id = $room_id;
        $model->participant = $participant;
        $model->time_start = $time_start;
        $model->time_end = $time_end;

        if (isset($_POST['RapatRequest'])) {
            $model->attributes = $_POST['RapatRequest'];
            if ($model->validate()) {
                // form inputs are valid, do something here
                if($model->save()) {
                    $this->redirect(array('request/list'));
                }
            }
        }

        $this->render('create', array(
            'model' => $model,
            'ruangRapat' => $ruangRapat));
    }
    
    public function actionUpdate($id) {
        $model = Rapat::model()->find('id = ' . $id);
        $ruangRapat = new Ruangan;
        $ruangRapat = $ruangRapat->findAll('type_ruangan_id = 1');
        
        if (isset($_POST['Rapat'])) {
            $model->attributes = $_POST['Rapat'];
            if ($model->validate()) {
                // form inputs are valid, do something here
                $model->save();
                $this->redirect(array('request/list'));
            }
        }

        $this->render('create', array(
            'model' => $model,
            'ruangRapat' => $ruangRapat));
    }
    
    public function actionDelete($id) {
        $model = Rapat::model()->find('id = ' . $id);
        if($model != null) {
            // Model is found
            if($model->delete()) {
                // Delete is success
                $this->redirect(Yii::app()->user->returnUrl);
            }
        }
    }

    public function actionList($userId = 12) {
        $model = new RapatRequest('search');
        $model->unsetAttributes();
        if (isset($_GET['RapatRequest'])) {
            $model->attributes = $_GET['RapatRequest'];
        }

        $this->render('list', array('model' => $model, 'dataProvider' => $model->searchByUser($userId)));
    }

    public function actionManage($status = null) {
        $model = new RapatRequest('search');
        $model->unsetAttributes();
        if (isset($_GET['RapatRequest'])) {
            $model->attributes = $_GET['RapatRequest'];
        }

        if ($status != null) {
            $this->renderPartial('request', array('model' => $model, 'dataProvider' => $model->searchRequest($status), 'status' => $status));
        } else {
            $this->render('admin', array('model' => $model));
        }
    }

    public function actionIndex() {
        $this->render('index');
    }

    public function actionReject($id) {
        if (Yii::app()->request->isPostRequest) {
            // we only allow deletion via POST request
            $model = $this->loadModel($id);
            $model->status = 'rejected';
            
            $rejectedMessage = $model->rejectedMessage;
            
            if($rejectedMessage == null && isset($_POST["message"])) {
                $rejectedMessage = new RejectedMessage();
            }
            
            $rejectedMessage->message = $_POST["message"];
            $rejectedMessage->rapat_id = $model->id;
            
            if(!$rejectedMessage->save()) {
                throw new CHttpException(400, 'Message can\'t save or updated.');
            }
            
            if (!$model->save()) {
                throw new CHttpException(400, 'request can\'t updated');
            }

            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if (!isset($_GET['ajax'])) {
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
            }
        } else {
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }
    }
    
    public function actionView($id) {
            $this->render('view',array(
			'model'=> RapatRequest::model()->findByPk((int) $id),
		));
    }
    
    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($id) {
        $model = Rapat::model()->findByPk((int) $id);
        if ($model === null) {
            throw new CHttpException(404, 'The requested page does not exist.');
        }
        return $model;
    }

    // Uncomment the following methods and override them if needed
    /*
    public function filters() {
        // return the filter configuration for this controller, e.g.:
        return array(
            'inlineFilterName',
            array(
                'class' => 'path.to.FilterClass',
                'propertyName' => 'propertyValue',
            ),
        );
    }

    public function actions() {
        // return external action classes, e.g.:
        return array(
            'action1' => 'path.to.ActionClass',
            'action2' => array(
                'class' => 'path.to.AnotherActionClass',
                'propertyName' => 'propertyValue',
            ),
        );
    } 
    */
}